package com.nnxy.worktostudy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnxy.worktostudy.system.dto.response.ApplyListItem;
import com.nnxy.worktostudy.system.dto.response.BillListItem;
import com.nnxy.worktostudy.system.dto.response.ProjectJobItem;
import com.nnxy.worktostudy.system.entity.Apply;
import com.nnxy.worktostudy.system.entity.Bill;
import com.nnxy.worktostudy.system.entity.Job;
import com.nnxy.worktostudy.system.entity.Project;
import com.nnxy.worktostudy.system.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : lu'qz
 * @create 2021/10/15 17:59
 */
@Service
public class FundServiceImpl implements FundService {
    @Resource
    private ProjectService projectService;
    @Resource
    private JobService jobService;
    @Resource
    private BillService billService;
    @Resource
    private StudentService studentService;
    @Resource
    private AdminService adminService;
    @Resource
    private ApplyService applyService;

    @Override
    public List<ProjectJobItem> getJobTree() {
        List<ProjectJobItem> projectJobTrees = new ArrayList<>();

        for (Project project: projectService.list()) {

            ProjectJobItem projectJobItem = new ProjectJobItem();
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();

            projectJobItem.setId(project.getId());
            projectJobItem.setName(project.getName());
            queryWrapper.eq("project_id", project.getId());
            projectJobItem.setJobs(jobService.list(queryWrapper));

            projectJobTrees.add(projectJobItem);
        }

        return projectJobTrees;
    }

    @Override
    public BillService getBillService() {
        return this.billService;
    }

    @Override
    public Map<String,Object> page(Wrapper<Bill> queryWrapper, Integer pageSize, Integer currentPage) {
        IPage<Bill> page = new Page<>();
        Map<String,Object> result = new HashMap<>(3);
        List<BillListItem> billListItems = new ArrayList<>();

        page.setSize(pageSize);
        page.setCurrent(currentPage);
        page = billService.page(page,queryWrapper);

        result.put("currentPage", page.getCurrent());
        result.put("total", page.getTotal());

        for (Bill bill: page.getRecords()) {
            BillListItem billListItem = new BillListItem();
            QueryWrapper<Apply> applyQueryWrapper = new QueryWrapper<>();

            applyQueryWrapper.and(wrapper -> wrapper.eq("student_id", bill.getStudentId()).eq("job_id", bill.getJobId()));

            billListItem.setId(bill.getId());
            billListItem.setTime(bill.getCreateTime());
            billListItem.setExpense(bill.getExpense());
            billListItem.setAdminId(bill.getAdminId());
            billListItem.setJobApplyTime(applyService.getOne(applyQueryWrapper).getCreateTime());
            billListItem.setAdminName(adminService.getById(bill.getAdminId()).getNickName());
            billListItem.setProject(projectService.getById(bill.getProjectId()));
            billListItem.setJob(jobService.getById(bill.getJobId()));
            billListItem.setStudent(studentService.getById(bill.getStudentId()));

            billListItems.add(billListItem);
        }

        result.put("data", billListItems);

        return result;
    }

    @Override
    public Map<String, Object> getProjectList(Integer pageSize, Integer currentPage) {
        IPage<Project> page = new Page<>();
        Map<String,Object> result = new HashMap<>(3);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("status", 0);
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        page = projectService.page(page, queryWrapper);

        result.put("currentPage", page.getCurrent());
        result.put("total", page.getTotal());
        result.put("data", page.getRecords());

        return result;
    }

    @Override
    public Boolean auditProject(Long id, Integer status) {
        UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        return projectService.update(updateWrapper);
    }
}
